package com.yifan.servlet;

import com.alibaba.fastjson.JSON;
import com.yifan.entity.User;
import com.yifan.service.UserService;
import com.yifan.utils.VCodeGenerator;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/registration")
public class LoginServlet extends HttpServlet{
	
	UserService userService = new UserService();
	private String vcode;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求的方法
		String method = request.getParameter("method");
		System.out.println(method);
		//请求分发
		if("getLogin".equals(method)){
			getLogin(request, response);
		}else if("login".equals(method)) {
			Login(request, response);
		}else if("logout".equals(method)) {
			logout(request, response);
		}else if("getVCode".equals(method)){
			getVCode(request, response);
		}
	}

	private void Login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println(999);
		System.out.println(666);
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String nvcode = request.getParameter("vcode");
		String role = request.getParameter("role");
		System.out.println("sdfa"+nvcode + role + username + password);


		Map<String,Object> map = new HashMap<String, Object>();
		if(vcode != null) {
			if(nvcode.toLowerCase().equals(vcode.toLowerCase())) {

					User user = userService.getUser(username, password);
					if(user != null) {
						request.getSession().setAttribute("userid", user.getId());
						request.getSession().setAttribute("u_p", username+","+password);
						request.getSession().setAttribute("username", username);
						request.getSession().setAttribute("role", user.getUserType());
						map.put("count", 1);
						map.put("role", user.getUserType());
						map.put("msg", "ok");
					}else {
						map.put("count", 2);
						map.put("msg", "用户名或密码不正确");
					}


			}else {
				map.put("count", 0);
				map.put("msg", "验证码错误");
			}
		}
		System.out.println(map);
		response.getWriter().write(JSON.toJSON(map).toString());
		
	}

	private void getLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.sendRedirect("/rjfxsys/login.jsp");
	}
	
	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.getSession().setAttribute("userid", null);
		request.getSession().setAttribute("u_p", null);
		request.getSession().setAttribute("rolename", null);
		request.getSession().setAttribute("username", null);
		response.sendRedirect("/rjfxsys/login.jsp");
	}
	
	private void getVCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//创建验证码生成器对象
		VCodeGenerator vcGenerator = new VCodeGenerator();
		//生成验证码
		vcode = vcGenerator.generatorVCode();
		//将验证码保存在session域中,以便判断验证码是否正确
		request.getSession().setAttribute("vcode", vcode);
		//生成验证码图片
		BufferedImage vImg = vcGenerator.generatorRotateVCodeImage(vcode, true);
		//输出图像
		ImageIO.write(vImg, "gif", response.getOutputStream());
	}
}
